package com.thinkfit.jy.fragment;

import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.constraintlayout.widget.ConstraintLayout;

import com.blankj.utilcode.util.GsonUtils;
import com.common.lib.constant.Constants;
import com.common.lib.constant.EventBusEvent;
import com.common.lib.fragment.BaseFragment;
import com.common.lib.mvp.contract.EmptyContract;
import com.common.lib.mvp.presenter.EmptyPresenter;
import com.common.lib.utils.BaseUtils;
import com.github.mikephil.charting.charts.CombinedChart;
import com.github.mikephil.charting.charts.LineChart;
import com.github.mikephil.charting.components.AxisBase;
import com.github.mikephil.charting.components.Description;
import com.github.mikephil.charting.components.Legend;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.components.YAxis;
import com.github.mikephil.charting.data.CombinedData;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.data.LineData;
import com.github.mikephil.charting.data.LineDataSet;
import com.github.mikephil.charting.formatter.ValueFormatter;
import com.thinkfit.jy.R;
import com.thinkfit.jy.serialport.SerialPortManager;
import com.thinkfit.jy.utils.DeviceTypeUtils;
import com.thinkfit.jy.utils.LogUtils;
import com.thinkfit.jy.utils.MotionDataUtils;
import com.thinkfit.jy.utils.SportsStatusBarViewUtils;
import com.thinkfit.jy.utils.Utils;

import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

/**
 * 自由运动信息卡片界面
 */
public class FreeMovementItem1Fragment extends BaseFragment<EmptyContract.Presenter> implements EmptyContract.View {

    int type = 0;
    float num = 0;
    LineChart lc_1;

    List<Entry> listSpeeds =new ArrayList<>();
    List<Entry> listSlops =new ArrayList<>();
    public static FreeMovementItem1Fragment getInstance(int type, float num) {
        Bundle bundle = new Bundle();
        bundle.putInt(Constants.BUNDLE_EXTRA, type);
        bundle.putFloat(Constants.BUNDLE_EXTRA_2, num);
        FreeMovementItem1Fragment fragment = new FreeMovementItem1Fragment();
        fragment.setArguments(bundle);
        return fragment;
    }
    @NonNull
    @Override
    protected EmptyContract.Presenter onCreatePresenter() {
        return new EmptyPresenter(this);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_free_movement_item_1;
    }


    @Override
    protected void initView(@NonNull View view, @Nullable Bundle savedInstanceState) {
        type = getArguments().getInt(Constants.BUNDLE_EXTRA);
        num = getArguments().getFloat(Constants.BUNDLE_EXTRA_2);
        lc_1 = view.findViewById(R.id.lc_1);
        lc_1.setEnabled(false);
        initLine(lc_1);
    }

    public void initLine(LineChart lc){

//        LineDataSet lineDataSet=new LineDataSet(list,title);
//
//        lineDataSet.setColor(lineColor);
//        lineDataSet.setDrawCircles(false);//折线上的小圆点
//        lineDataSet.setMode(LineDataSet.Mode.CUBIC_BEZIER);//这里是圆滑曲线
//        lineDataSet.setHighLightColor(getColor(R.color.white));
//        lineDataSet.setValueTextColor(getColor(R.color.white));
//        lineDataSet.setValueTextSize(0);

        // 设置X轴可见范围的最大值
        lc.setVisibleXRangeMaximum(100);

        // 设置Y轴可见范围的最大值
        lc.setVisibleYRangeMaximum(100, YAxis.AxisDependency.LEFT);

        lc.getXAxis().setDrawGridLines(false);
        lc.getXAxis().setDrawAxisLine(true);
        //是否绘制右侧轴线
        lc.getAxisRight().setEnabled(false);
        lc.setTouchEnabled(false);
        lc.getAxisLeft().setDrawGridLines(false);
        lc.getAxisRight().setDrawGridLines(false);

        lc.getXAxis().setAxisMinimum(0f);
        lc.getAxisLeft().setAxisMinimum(0f);
        lc.getXAxis().setTextColor(getContext().getColor(R.color.white));
        lc.getAxisLeft().setTextColor(getContext().getColor(R.color.white));
        //设置x轴的位置
        lc.getXAxis().setPosition(XAxis.XAxisPosition.BOTTOM);
//        lc.getXAxis().setValueFormatter(new ValueFormatter() {
//            @Override
//            public String getFormattedValue(float value, AxisBase axis) {
//                return super.getFormattedValue(value, axis);
//            }
//        });;
        Description description=new Description();
        description.setEnabled(false);
        lc.setDescription(description);


        //对legend的一些设置
        //得到legend对象
        Legend legend=lc.getLegend();
        legend.setEnabled(false);
        legend.setTextColor(Color.RED);


        LineData lineData = new LineData();



        lc.setData(lineData);

    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onReceive(HashMap<String, Object> map) {
        if (map == null) {
            return;
        }

        if (map.containsKey(EventBusEvent.START_RUN)) {
            ArrayList<Integer> target = SportsStatusBarViewUtils.getInstance().getTarget();
            type = Integer.valueOf(target.get(0));
            num = Integer.valueOf(target.get(1));
//            if(type == 0){
//                tvSportsCenter.setText(getString(R.string.app_free_training));
//            }else {
//                tvSportsCenter.setText(SportsStatusBarViewUtils.getInstance().targetType(type, num));
//            }
        }else if (map.containsKey(EventBusEvent.STOP_RUN)){
//            clSportInfo.setVisibility(View.GONE);
//            tvSportsCenter.setText(getString(R.string.app_sports_center));
            num = 0;
            type = 0;
//            stopInitView();
        }else if(map.containsKey(EventBusEvent.PAUSE_RUN)){

        }else if (map.containsKey(EventBusEvent.DEVICE_DATA)) {
            System.out.println("MotionDataUtils -------------> ddddddddddddddddddddddd  CurrentInfo : "+ GsonUtils.toJson((SerialPortManager.CurrentInfo) map.get(EventBusEvent.DEVICE_DATA)));
            SerialPortManager.CurrentInfo currentInfo = (SerialPortManager.CurrentInfo) map.get(EventBusEvent.DEVICE_DATA);
            resetInfo(currentInfo);
        }else if(map.containsKey(EventBusEvent.EQUIPMENT_STATE)){//设备状态
//            SerialPortManager.CurrentInfo currentInfo = (SerialPortManager.CurrentInfo) map.get(EventBusEvent.EQUIPMENT_STATE);
            SerialPortManager.CurrentInfo currentInfo = MotionDataUtils.getInstance().getCurrentInfo();
            resetInfo(currentInfo);
        }else if(map.containsKey(EventBusEvent.SET_TARGET)){
            ArrayList<String> o = (ArrayList<String>) map.get(EventBusEvent.SET_TARGET);
            type = Integer.valueOf(o.get(0));
            num = Integer.valueOf(o.get(1));

        }
    }

    int time = 0;
    public void resetInfo(SerialPortManager.CurrentInfo info) {

        float kcal = info.kcal /10f;
        int hour = info.time / 3600;
        int minute = (info.time / 60) % 60;
//        int minute = 0;
        int second = info.time % 60;

        float v = (60f / (info.speed/10f))*60;
        int paceHour = (int) (v/3600);
        int paceMin = (int) ((v/ 60) % 60);
        int paceSecond = (int) v % 60;
        if(time < info.time){
            time = info.time;
            if(DeviceTypeUtils.getInstance().isTreadmill()){
                listSpeeds.add(new Entry(time,(info.speed/10f)));
                listSlops.add(new Entry(time,(info.slop*1f)));
                setLineData(lc_1,listSpeeds,listSlops,getContext().getString(R.string.app_speed_txt),getContext().getString(R.string.app_slop)
                        ,getContext().getColor(R.color.color_4865B5),getContext().getColor(R.color.color_3BC2AC));
            }else {
                listSpeeds.add(new Entry(time,(info.speed/100f)));
                listSlops.add(new Entry(time,(info.slop*1f)));
                setLineData(lc_1,listSpeeds,listSlops,getContext().getString(R.string.app_speed_txt),getContext().getString(R.string.app_resistance)
                        ,getContext().getColor(R.color.color_4865B5),getContext().getColor(R.color.color_3BC2AC));

            }

        }

    }

    /**
     *
     * @param lc
     * @param speeds 速度集
     * @param slops 坡度集
     * @param titleSpeed 速度标题
     * @param titleSlop 坡度标题
     * @param lineSpeedColor 速度曲线颜色
     * @param lineSlopColor  坡度曲线颜色
     */
    public void setLineData(LineChart lc,List<Entry> speeds,List<Entry> slops,String titleSpeed,String titleSlop,int lineSpeedColor,int lineSlopColor){
        LineDataSet lineDataSet=new LineDataSet(speeds,titleSpeed);
        lineDataSet.setColor(lineSpeedColor);
        lineDataSet.setLineWidth(3);
        lineDataSet.setDrawCircles(false);//折线上的小圆点
        lineDataSet.setMode(LineDataSet.Mode.HORIZONTAL_BEZIER);//这里是圆滑曲线
        lineDataSet.setHighLightColor(getContext().getColor(R.color.white));
        lineDataSet.setValueTextColor(getContext().getColor(R.color.white));
        lineDataSet.setValueTextSize(0);

        LineDataSet lineDataSet1=new LineDataSet(slops,titleSlop);
        lineDataSet1.setColor(lineSlopColor);
        lineDataSet1.setFillDrawable(getContext().getDrawable(R.drawable.shape_gradient_3bc2ac_203bc2ac_270));
        lineDataSet1.setLineWidth(3);
        lineDataSet1.setDrawCircles(false);//折线上的小圆点
        lineDataSet1.setMode(LineDataSet.Mode.CUBIC_BEZIER);//这里是圆滑曲线
//        lineDataSet1.setHighLightColor(getContext().getColor(R.color.white));
//        lineDataSet1.setValueTextColor(getContext().getColor(R.color.white));
        lineDataSet1.setValueTextSize(0);
        LineData lineData = new LineData(lineDataSet1,lineDataSet);

        lc.clearValues();


        lc.setData(lineData);
    }

    @Override
    public void onStop() {
        super.onStop();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {

        }
    }

}
